<?php
namespace Module\Controller;


use Common\Controller\AdminController;
use Common\Model\ModuleModel;

class ManageController extends AdminController {

    // 模块管理
    public function lists() {
        $model = new ModuleModel();
        $modules = [];
        foreach (glob('Addons/*') as $dir) {
            // 合法模块验证
            if (is_dir($dir) && is_file($dir . '/manifest.php')) {
                $modules[] = include $dir . '/manifest.php';
            }
        }
        // 安装了的模块列表
        $hasModuleList = $model->getField('id,name');

        $this->hasModuleList = $hasModuleList;

        $this->modules = $modules;
        $this->display();
    }

    // 安装模块
    public function install() {
        $name = I('name', '', 'ucfirst');
        $model = new ModuleModel();
        // 模块是否存在的检测
        $ret = $model->where(['title' => $name])->count();
        if ($ret > 0) {
            $this->error('模块已安装,请不要重复安装!');
            exit;
        }
        // 开始安装模块
        $data = include 'Addons/' . $name . '/manifest.php';
        $data['actions'] = json_encode($data['actions'],JSON_UNESCAPED_UNICODE);

        $this->store($model,$data);
    }

    // 卸载模块
    public function uninstall() {
        $name = I('name', '');
        $model = new ModuleModel();
        if($model->where(['name'=>$name])->delete()){
            $this->success('卸载模块成功!',U('lists'));
        }else{
            $this->error('卸载模块失败!');
        }
    }

    // 模块设计
    public function design() {

        if (IS_POST) {
            $data = I('post.');
            $moduleName = ucfirst($data['name']);
            // 创建模块目录和基本文件
            // 创建模块目录
            $dirPath = 'Addons/' . $moduleName;
            $moduleTemplatePath = 'Data/ModuleTemplate/*.tpl';
            if (is_dir($dirPath)) {
                $this->error('目录已存在,不允许重复创建!');
                exit;
            }
            mkdir($dirPath . '/View', 0755, true);
            foreach (glob($moduleTemplatePath) as $file) {
                $d = file_get_contents($file);
                $fileName = basename($file, '.tpl') . '.php';
                $d = str_replace('{NAME}', $moduleName, $d);
                file_put_contents($dirPath . '/' . $fileName, $d);
            }

            // 创建基本文件
            $this->mainfest($data, $dirPath . '/manifest.php');

            $this->success('创建成功!', u('lists'));

            exit;
        }

        $this->display();
    }


    /**
     * 创建基本文件
     * @param $data
     * @param $manifest
     * @return bool
     */
    private function mainfest($data, $manifest) {
        $actions = array_filter(preg_split('@(\r|\n)@', $data['actions']));
        $actionList = [];
        foreach ($actions as $v) {
            list($title, $name) = explode('|', $v);
            $actionList[] = [
                'title' => $title,
                'name' => $name
            ];
        }
        $data['actions'] = $actionList;
        $d = '<?php' . PHP_EOL . 'return ' . var_export($data, true) . ';' . PHP_EOL . '?>';
        file_put_contents($manifest, $d);
        return true;
    }

}